package com.lh.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lh.core.annotation.Authorization;
import com.lh.entity.dto.JyQueryDto;
import com.lh.entity.table.DsmsUser;
import com.lh.entity.table.RectificationInfo;
import com.lh.mapper.DsmsUserMapper;
import com.lh.service.DsmsUserService;
import com.lh.utils.Answer;
import com.lh.utils.HuangGangUtil;
import com.lh.utils.IHuangGangUtil;
import org.activiti.engine.impl.db.DbSqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.List;

import static com.lh.utils.HuangGangUtil.generateRandomNumber;

@RestController
@RequestMapping("/user")
public class DsmsUserController {

    private static final Logger log = LoggerFactory.getLogger(DsmsUserController.class);
    @Autowired
    private DsmsUserService dsmsUserService;

    @Resource
    private DsmsUserMapper dsmsUserMapper;

//    @Authorization
//    @OnlyAdmin
    @RequestMapping("/addUser")
    public Answer<Object> addUser(@RequestBody DsmsUser dsmsUser) {
        return  dsmsUserService.addUser(dsmsUser);
    }


    @RequestMapping("/editUser")
    public Answer<Object> editUser(@RequestBody DsmsUser dsmsUser) {
        return  dsmsUserService.editUser(dsmsUser);
    }


//    @Authorization
//    @OnlyAdmin
    @RequestMapping("/deleteUser")
    public Answer<Object> deleteUser(@RequestBody DsmsUser dsmsUser) {
        return  dsmsUserService.deleteUser(dsmsUser);
    }

//    @Authorization
    @RequestMapping(value = "/getUserList",method = RequestMethod.POST)
    public Answer<Object> getUserList(@RequestBody JyQueryDto jyQueryDto){
        return dsmsUserService.queryList(jyQueryDto);
    }

    @Authorization
    @RequestMapping(value = "/getQrCode",method = RequestMethod.POST)
    public Answer<Object> getQrCode() {
        String result = IHuangGangUtil.jyLogin();
        return Answer.ok(result);
    }


    @Authorization
    @RequestMapping(value = "/getAesResult",method = RequestMethod.POST)
    public Answer<Object> getAesResult(@RequestBody JSONObject jsonObject) {
        String transId = jsonObject.getString("transId");
        String signature = jsonObject.getString("signature");
        String deviceId = jsonObject.getString("deviceId");
        String signValue = jsonObject.getString("signValue");
        String certDN = jsonObject.getString("certDN");
        String certSN = jsonObject.getString("certSN");
        String issuerDN = jsonObject.getString("issuerDN");
        log.info("certDN: "+certDN);
        log.info("certSN: "+certSN);
        log.info("issuerDN: "+issuerDN);
        log.info("signValue: "+signValue);
        log.info("deviceId: "+deviceId);
        log.info("signature: "+signature);
        log.info("transId: "+transId);
        String result = HuangGangUtil.getAesResult(signValue,transId,signature,deviceId);
        log.info("result: "+result);
        return Answer.ok(result);
    }

    /**
     * i黄冈登录 获取用户的UserId 和身份证号
     * @param oToken
     * @return
     */
    @Authorization
    @RequestMapping(value = "/getPhone",method = RequestMethod.POST)
    public Answer<Object> login(@RequestBody String oToken) {
        System.out.println("++++");
        JSONObject aesJsonBean = JSONObject.parseObject(oToken);
        System.out.println(aesJsonBean);
        String iHgToken = aesJsonBean.getString("oToken");
        System.out.println(iHgToken);
        String aesRes = HuangGangUtil.login(oToken);
        System.out.println(aesRes);
        JSONObject aesJsonBean2 = JSONObject.parseObject(aesRes);
        System.out.println(aesJsonBean2);
        if (aesJsonBean2!=null && aesJsonBean2.getString("userId")!=null){
            String mobilePhone = aesJsonBean2.getString("mobilePhone");
            return Answer.ok(mobilePhone);
        }
        return Answer.error("禁止登录");
    }


    //    getUserInfo
    @Authorization
    @RequestMapping(value = "/getUserInfo",method = RequestMethod.POST)
    public Answer<Object> getUserInfo(@RequestBody JSONObject jsonObject) {
        String userName = jsonObject.getString("userName");
        //
        if (StrUtil.isNotEmpty(userName)){
            List<DsmsUser> userList = dsmsUserService.queryUserByName(userName);
            if (userList!=null && userList.size()>0){
                return Answer.ok(userList.get(0));
            }
        }
        return Answer.error("禁止登录");

    }

    /**
     * i黄冈登录校验 扫码
     * 用户登录调取比对数据库是否存在（手机号和身份证号码），存在即可登录，返回user表，不存在抛异常
     */
    @Authorization
    @RequestMapping(value = "/loginCheck",method = RequestMethod.POST)
    public Answer<Object> loginCheck(@RequestBody String uuid) {
        JSONObject aesJsonBean = JSONObject.parseObject(uuid);
        String UID = aesJsonBean.getString("uuid");
        String data = IHuangGangUtil.getCodeResult(UID);
        if (ObjectUtils.isEmpty(data)){
            return Answer.error("禁止登录");
        }else{
            JSONObject jsonObject = JSONObject.parseObject(data);
            if (jsonObject.getIntValue("code") == 1){
                String dataStr = jsonObject.getString("data");
                System.out.println(dataStr);
                JSONObject dataJson = JSONObject.parseObject(dataStr);
                String userType = dataJson.getString("userType");
                System.out.println(userType);
                String userPhone = "";
                if (userType!=null && userType.equals("1")){
                    userPhone = dataJson.getString("mobilePhone");
                }else if (userType!=null && userType.equals("2")){
                    userPhone = dataJson.getString("agentMobile");
                }
                DsmsUser user = dsmsUserMapper.selectOne(new LambdaQueryWrapper<DsmsUser>().eq(DsmsUser::getMobileNo, userPhone));
                if (ObjectUtils.isEmpty(user)){
                    return Answer.error("禁止登录");
                }else{
                    return Answer.ok(user);
                }
            }
            return Answer.error("禁止登录");
        }
    }



}
